//
//  ANShoppingCartEndView.m
//  Ants
//
//  Created by ZHM on 16/9/7.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANShoppingCartEndView.h"

@interface ANShoppingCartEndView ()
@property (nonatomic, strong) UIButton *deleteBut;
@property (nonatomic, strong) UIButton *pushBut;
@property (nonatomic, weak) UIToolbar *toolbar;
@property (nonatomic, weak) UIImageView *line;
@end

#define optionsViwHeight 44

@implementation ANShoppingCartEndView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        [self addObserver:self forKeyPath:@"isEdit" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    }
    return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"isEdit"]) {
        if (self.isEdit) {
            _priceLab.hidden = YES;
            _deleteBut.hidden = NO;
            _pushBut.hidden = YES;
        }
        else {
            _priceLab.hidden = NO;
            _deleteBut.hidden = YES;
            _pushBut.hidden = NO;
        }
    }
}
+ (instancetype)shoppingCartEndViewWithVC:(id)delegate {
    UIViewController *vc = (UIViewController *)delegate;
    CGFloat buyY;
    if (vc.hidesBottomBarWhenPushed) {
        buyY = CGRectGetMaxY(ANKeyWindow.frame) - optionsViwHeight;
    } else {
        buyY = CGRectGetMaxY(ANKeyWindow.frame) - optionsViwHeight - 49;
    }
    ANShoppingCartEndView *buyCV = [[ANShoppingCartEndView alloc] initWithFrame:CGRectMake(0,buyY, ANKeyWindow.bounds.size.width, optionsViwHeight)];
    buyCV.delegate = delegate;
    [buyCV addUpAllChildView];
    [vc.view addSubview:buyCV];
    return buyCV;
}
- (void)addUpAllChildView {
    
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;
    _toolbar = toolbar;
    [self addSubview:toolbar];
    
    UIImage *unSelectedImage = [UIImage imageNamed:@"Unselected.png"];
    UIImage *selectImage = [UIImage imageNamed:@"Selected.png"];
    _selectBut = [[UIButton alloc] init];
    _selectBut.selected = NO;
    [_selectBut addTarget:self action:@selector(clickAllEnd:) forControlEvents:UIControlEventTouchUpInside];
    [_selectBut setImage:unSelectedImage forState:UIControlStateNormal];
    [_selectBut setImage:selectImage forState:UIControlStateSelected];
    [_selectBut setTitle:@" 全选" forState:UIControlStateNormal];
    _selectBut.titleLabel.font =[UIFont systemFontOfSize:13];
    [_selectBut setTitle:@" 全选" forState:UIControlStateSelected];
    [_selectBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_selectBut setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [self addSubview:_selectBut];
    
    _priceLab = [[UILabel alloc]init];
    _priceLab.textColor = [UIColor blackColor];
    _priceLab.text = [NSString stringWithFormat:@"合计: ￥ 0"];
    _priceLab.font = [UIFont systemFontOfSize:15];
    [self addSubview:_priceLab];
    
    _pushBut = [[UIButton alloc]init];
    _pushBut.hidden = NO;
    _pushBut.tag = 18;
    [_pushBut setTitle:@"结算" forState:UIControlStateNormal];
    _pushBut.titleLabel.font = [UIFont systemFontOfSize:14];
    _pushBut.backgroundColor = [UIColor orangeColor];
    [[_pushBut layer]setCornerRadius:3.0];
    [_pushBut addTarget:self action:@selector(clickRightBut:) forControlEvents:UIControlEventTouchUpInside];
    [_pushBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self addSubview:_pushBut];
    
    _deleteBut = [[UIButton alloc]init];
    _deleteBut.hidden = YES;
    [_deleteBut setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    _deleteBut.tag=19;
    [_deleteBut setTitle:@"删除" forState:UIControlStateNormal];
    _deleteBut.titleLabel.font=[UIFont systemFontOfSize:14];
    _deleteBut.backgroundColor=[UIColor whiteColor];
    [[_deleteBut layer]setCornerRadius:3.0];
    [_deleteBut.layer setBorderWidth:0.5];
    [_deleteBut addTarget:self action:@selector(clickRightBut:) forControlEvents:UIControlEventTouchUpInside];
    _deleteBut.layer.borderColor = [[UIColor orangeColor] CGColor];
    [self addSubview:_deleteBut];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, optionsViwHeight - 0.5, ANscreenW, 0.5)];
    line.backgroundColor = ANToopColor;
    [self addSubview:line];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    _toolbar.frame = self.bounds;
    
    UIImage *selectImage = [UIImage imageNamed:@"Selected.png"];
    _selectBut.frame = CGRectMake(3.5, (self.height - selectImage.size.height) / 2, selectImage.size.width + 40, selectImage.size.height);
    _priceLab.frame = CGRectMake(CGRectGetMaxX(_selectBut.frame) + 20, 0, 150, self.height);
    _pushBut.frame = CGRectMake(self.width - 10 - 80, (self.height - 30) / 2, 80, 30);
    _deleteBut.frame = _pushBut.frame;

}
-(void)clickAllEnd:(UIButton *)but {
    if ([self.delegate respondsToSelector:@selector(clickALLEnd:)]) {
        [self.delegate clickALLEnd:but];
    }
}
- (void)clickRightBut:(UIButton *)but {
    if ([self.delegate respondsToSelector:@selector(clickRightBut:)]) {
        [self.delegate clickRightBut:but];
    }
}
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"isEdit"];
}
@end
